<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button
        class="filter-item"
        style="margin-left: 10px;"
        type="primary"
        icon="el-icon-plus"
        @click="handleEdit"
      >
        Add
      </el-button>
    </div>
    <!-- 请注意，行键是获得正确行顺序所必需的。 Note that row-key is necessary to get a correct row order. row-key="id"-->
    <el-table
      ref="dragTable"
      v-loading="listLoading"
      :data="list"
      border
      highlight-current-row
      style="width: 100%"
      size="mini"
    >
      <el-table-column align="center" label="ID" width="65">
        <template slot-scope="{row}">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="轮播图">
        <template slot-scope="{row}">
          <el-image
            style="width: 50px; height: 50px"
            :src="baseStatic + row.img_src"
            :preview-src-list="[baseStatic + row.img_src]"
          />
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="120">
        <el-row>
          <el-button type="primary" icon="el-icon-edit" circle @click="handleEdit" />
          <el-button type="danger" icon="el-icon-delete" circle @click="handleDelete" />
        </el-row>
      </el-table-column>
    </el-table>

    <el-dialog title="编辑" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="轮播图" label-width="120px">
          <el-input v-model="form.img_src" autocomplete="off" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogFormVisible = false">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { fetchList } from '@/api/banner'
import Sortable from 'sortablejs'

export default {
  name: 'Banner',
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'info',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 10
      },
      sortable: null,
      dialogFormVisible: false,
      form: {
        img_src: '123.jpg'
      },
      baseStatic: `${process.env.VUE_APP_BASE_API}static/`
    }
  },
  created() {
    this.getList()
  },
  methods: {
    async getList() {
      this.listLoading = true
      const { data } = await fetchList(this.listQuery)
      this.list = data[0] // data[1] 总条数
      this.listLoading = false
      this.$nextTick(() => {
        this.setSort()
      })
    },
    setSort() {
      const el = this.$refs.dragTable.$el.querySelectorAll('.el-table__body-wrapper > table > tbody')[0]
      this.sortable = Sortable.create(el, {
        ghostClass: 'sortable-ghost', // 放置占位符的类名， Class name for the drop placeholder,
        setData: function(dataTransfer) {
          // 避免Firefox错误 to avoid Firefox bug
          // Detail see : https://github.com/RubaXa/Sortable/issues/1012
          dataTransfer.setData('Text', '')
        },
        onEnd: evt => {
          console.log('修改order_number')

          // const targetRow = this.list.splice(evt.oldIndex, 1)[0]
          // this.list.splice(evt.newIndex, 0, targetRow)

          // // 为了显示更改，可以在代码中删除 for show the changes, you can delete in you code
          // const tempIndex = this.newList.splice(evt.oldIndex, 1)[0]
          // this.newList.splice(evt.newIndex, 0, tempIndex)
        }
      })
    },
    handleEdit() {
      console.log('修改')
      // this.dialogFormVisible = true;
    },
    handleDelete() {
    }
  }
}
</script>

<style>
.sortable-ghost {
    opacity: .8;
    color: #fff !important;
    ckground: #42b983 !important;
}
</style>

<style scoped>
.icon-star {
    margin-right: 2px;
}

.drag-handler {
    width: 20px;
    height: 20px;
    cursor: pointer;
}

.show-d {
    margin-top: 15px;
}
</style>
